#include "stm32f10x.h"                  // Device header

void adc1_init()
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	RCC_ADCCLKConfig(RCC_PCLK2_Div6);
	
	GPIO_InitTypeDef adc_gpio_init;
	adc_gpio_init.GPIO_Mode=GPIO_Mode_AIN;
	adc_gpio_init.GPIO_Pin=GPIO_Pin_0;
	adc_gpio_init.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&adc_gpio_init);
	
	
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);
	ADC_InitTypeDef adc_init;
	adc_init.ADC_ContinuousConvMode=DISABLE;
	adc_init.ADC_DataAlign=ADC_DataAlign_Right;
	adc_init.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
	adc_init.ADC_Mode=ADC_Mode_Independent;
	adc_init.ADC_NbrOfChannel=1;
	adc_init.ADC_ScanConvMode=DISABLE;
	ADC_Init(ADC1,&adc_init);
	
	ADC_Cmd(ADC1,ENABLE);
	
	ADC_ResetCalibration(ADC1);
	while (ADC_GetResetCalibrationStatus(ADC1));
	ADC_StartCalibration(ADC1);
	while (ADC_GetCalibrationStatus(ADC1));
}


int get_adc()
{
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)== RESET);
	return ADC_GetConversionValue(ADC1);

}






